/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package combatlogparser.parser;

import combatlogparser.parser.LogImport;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Administrator
 */
public class ParseCombatLog implements LogImport {
//4/1 09:48:43.296  SPELL_CAST_FAILED,0x0000000000652C72,"Nkids",0x511,0x0000000000000000,nil,0x80000000,51953,"黑铁的烟草",0x1,"物品还没有准备好"

	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("M/d HH:mm:ss.SSS");
	//private static final Pattern pattern = Pattern.compile("^(\\d{1,}\\/\\d{1,}\\s\\S+)\\s(.*)");
	private static final Pattern pattern = Pattern.compile("^(\\d{1,}\\/\\d{1,}\\s\\S+)\\s(.*)");
	private Set<String> eventSet = new HashSet<String>();

	public void read(String logPath) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(logPath), "utf-8"));
		String line;
		while ((line = in.readLine()) != null) {
//			Matcher matcher = pattern.matcher(line);
//			if (matcher.matches()) {
//				System.out.println(matcher.group(1));
//			}
			parseLine(line);
		}

		Iterator<String> envntIter = eventSet.iterator();
		while (envntIter.hasNext()) {
			System.out.println(envntIter.next());
		}
	}

	public CombatEvent parseLine(String line) throws Exception {
		if (line == null || line.trim().length() == 0) {
			return null;
		}
		Matcher matcher = pattern.matcher(line);
		if (!matcher.matches() && matcher.groupCount() != 2) {
			return null;
		}
		CombatEvent combatEvent = null;
		String[] eventArray = fixQuotes(matcher.group(2).trim().split(","));
		String eventName = eventArray[0];
		if (eventName.equals("DAMAGE_SHIELD")) {
			return combatEvent;
		}
		if (eventName.equals("DAMAGE_SHIELD_MISSED")) {
			return combatEvent;
		}
		if (eventName.equals("ENCHANT_APPLIED")) {
			return combatEvent;
		}
		if (eventName.equals("ENCHANT_REMOVED")) {
			return combatEvent;
		}
		if (eventName.equals("ENVIRONMENTAL_DAMAGE")) {
			return combatEvent;
		}
		if (eventName.equals("PARTY_KILL")) {
			return combatEvent;
		}
		if (eventName.equals("RANGE_DAMAGE")) {
			return combatEvent;
		}
		if (eventName.equals("RANGE_MISSED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_APPLIED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_APPLIED_DOSE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_BROKEN")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_BROKEN_SPELL")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_REFRESH")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_REMOVED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_AURA_REMOVED_DOSE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_CAST_FAILED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_CAST_START")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_CAST_SUCCESS")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_CREATE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_DAMAGE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_DISPEL")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_DISPEL_FAILED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_ENERGIZE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_EXTRA_ATTACKS")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_HEAL")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_INTERRUPT")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_LEECH")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_MISSED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_PERIODIC_DAMAGE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_PERIODIC_ENERGIZE")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_PERIODIC_HEAL")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_PERIODIC_LEECH")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_PERIODIC_MISSED")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_RESURRECT")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_STOLEN")) {
			return combatEvent;
		}
		if (eventName.equals("SPELL_SUMMON")) {
			return combatEvent;
		}
		if (eventName.equals("SWING_DAMAGE")) {
			return combatEvent;
		}
		if (eventName.equals("SWING_MISSED")) {
			return combatEvent;
		}
		if (eventName.equals("UNIT_DIED")) {
			//5 target
			System.out.println(eventArray[5]);
			
			return combatEvent;
		}
		return null;
	}

	private String[] fixQuotes(String[] parts) {
		String[] newParts = new String[parts.length];
		int filed = 0;
		for (String f : parts) {
			char[] fc = f.toCharArray();
			if (fc[0] == '"' && fc[fc.length - 1] == '"') {
				f = new String(fc, 1, fc.length - 2);
			}
			newParts[filed] = f.trim();
			filed++;
		}
		return newParts;
	}
}
